package gao.xiaolei.service;

import java.util.Arrays;

import javax.annotation.Resource;

import gao.xiaolei.util.JwtUtil;
import gao.xiaolei.util.RedisUtil;
import org.springframework.stereotype.Service;

@Service("PageService")
public class PageService {

    @Resource(name = "RedisUtil")
    private RedisUtil redisUtil;

    public String getReturnPage(String access_token) {
        String[] roles = JwtUtil.getRoles(access_token);
        if (Arrays.stream(roles).anyMatch(r -> {
            return "ROLE_SUPERADMIN".equals(r);
        }))
            return "view/auth/admin";
        else if (Arrays.stream(roles).anyMatch(r -> {
            return "ROLE_ADMIN".equals(r);
        }))
            return "view/training/index-admin";
        else if (Arrays.stream(roles).anyMatch(r -> {
            return "ROLE_DIRECTOR".equals(r);
        }))
            return "view/training/index-director";
        else if (Arrays.stream(roles).anyMatch(r -> {
            return "ROLE_TEACHER".equals(r);
        }))
            return "view/training/index-teacher";
        else if (Arrays.stream(roles).anyMatch(r -> {
            return "ROLE_GROUP_LEADER".equals(r) || "ROLE_USER".equals(r);
        })) {//如果是组长或者说普通用户的话就要让他们选择进入哪个项目
            String userId = JwtUtil.getUserId(access_token);
            if (!redisUtil.hasKey(userId))//如果缓存里没有用户选择项目的记录就要让他去选择项目进入
                return "view/training/index-before";
            else {//如果缓存里有的话就要和access_token里的领导项目进行比对
                String inProject = (String) redisUtil.get(userId);
                String[] leaderProjects = JwtUtil.getLeaderProject(access_token);
                for (int i = 0, length = leaderProjects.length; i < length; i++) {
                    if (leaderProjects[i].equals(inProject))//如果选择进入的组是组长的话
                        return "view/training/index-hasCheck";//就返回组长的界面
                }
                return "view/training/index";//如果不是组长就返回不是组长的界面
            }
        } else
            return "view/login";//返回登陆界面（因为什么角色都不是）
    }
}
